package com.jd.dlc.domain;

import lombok.Builder;

import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @program: dlc
 * @ClassName WrapLimiterRule
 * @description: Current limiting rule encapsulation
 * @author: sizegang
 * @create: 2022-05-11
 **/

@Builder
public class WrapLimiterRule {


    private Long timeout;

    private Long limit;

    private Integer truncateStart;

    private Integer truncateEnd;

    private Integer gray;

    /**
     * 限流是否开启
     */
    private boolean enable;
    /**
     * 自动黑名单开关记录
     */
    private boolean recordLimit;

    /**
     * 自动黑名单 时间
     */
    private Integer recordTime;

    /**
     * 是否开启限流优化
     */
    private boolean advanced;

    /**
     * 触发多少次自动限流后被列入黑名单
     */
    private Long limitNum;


    /**
     * 触发限流
     */
    private boolean triggerLimit;

    /**
     * 黑名单
     */
    private Set<String> blackList;

    /**
     * 白名单
     */
    private Set<String> whiteList;


    public boolean isTriggerLimit() {
        return triggerLimit;
    }

    public void setTriggerLimit(boolean triggerLimit) {
        this.triggerLimit = triggerLimit;
    }

    public Set<String> getBlackList() {
        return blackList;
    }

    public void setBlackList(Set<String> blackList) {
        this.blackList = blackList;
    }

    public Set<String> getWhiteList() {
        return whiteList;
    }

    public void setWhiteList(Set<String> whiteList) {
        this.whiteList = whiteList;
    }

    public boolean isRecordLimit() {
        return recordLimit;
    }

    public void setRecordLimit(boolean recordLimit) {
        this.recordLimit = recordLimit;
    }

    public Integer getRecordTime() {
        return recordTime;
    }

    public void setRecordTime(Integer recordTime) {
        this.recordTime = recordTime;
    }

    public Long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public Long getLimit() {
        return limit;
    }

    public void setLimit(long limit) {
        this.limit = limit;
    }

    public void setTimeout(Long timeout) {
        this.timeout = timeout;
    }

    public void setLimit(Long limit) {
        this.limit = limit;
    }

    public Integer getTruncateStart() {
        return truncateStart;
    }

    public void setTruncateStart(Integer truncateStart) {
        this.truncateStart = truncateStart;
    }

    public Integer getTruncateEnd() {
        return truncateEnd;
    }

    public void setTruncateEnd(Integer truncateEnd) {
        this.truncateEnd = truncateEnd;
    }

    public Integer getGray() {
        return gray;
    }

    public void setGray(Integer gray) {
        this.gray = gray;
    }

    public boolean isAdvanced() {
        return advanced;
    }

    public void setAdvanced(boolean advanced) {
        this.advanced = advanced;
    }

    public Long getLimitNum() {
        return limitNum;
    }

    public void setLimitNum(Long limitNum) {
        this.limitNum = limitNum;
    }

    /**
     * for gray
     *
     * @return
     */
    public boolean isEnable() {

        if (!enable) {
            return enable;
        }

        if (gray == null) {
            return enable;
        }

        if (gray == null || gray.equals(0)) {
            return enable;
        }
        /**
         * Multithreading does not share
         */
        int index = ThreadLocalRandom.current().nextInt(100);

        if (index <= gray) {
            return true;
        }
        return false;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }


}
